package com.baaio.xml;

import java.io.FileOutputStream;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.baaio.util.GetRealPath;

/**
 * 生成config.xml配置文件的类
 * 
 * 
 */
public class WriterConfig
{

	/**
	 * 
	 * @param h
	 *            数据库地址
	 * @param p
	 *            数据库端口号
	 * @param d
	 *            数据库名
	 * @param r
	 *            用户名
	 * @param pw
	 *            密码
	 * @param sq
	 *            三个选择，一个是mysql,一个是sql,一个是oldsql
	 * @param pr
	 *            运行时控制端输出查询语句，true是，false否
	 */
	public WriterConfig(String h, String p, String d, String r, String pw,
			String sq, String pr)
	{

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try
		{
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.newDocument();
			Element root = document.createElement("config");
//			Comment sjkdz = document.createComment("数据库地址");
//			Comment dk = document.createComment("数据库端口号");
//			Comment sjkm = document.createComment("数据库名");
//			Comment name = document.createComment("用户名");
//			Comment mima = document.createComment("密码");
//			Comment xz = document.createComment(" 三个选择，一个是mysql驱动：com.mysql.jdbc.Drive，一个是sql驱动：com.microsoft.sqlserver.jdbc.SQLServerDriver，一个是oldsql驱动：com.microsoft.jdbc.sqlserver.SQLServerDriver。");
//			Comment sc = document.createComment("运行时控制端输出查询语句，true是，false否 ");
			document.appendChild(root);
			Element host = document.createElement("host");
			host.setTextContent(h);
//			root.appendChild(sjkdz);
			root.appendChild(host);

			Element port = document.createElement("port");
			port.setTextContent(p);
//			root.appendChild(dk);
			root.appendChild(port);

			Element db = document.createElement("db");
			db.setTextContent(d);
//			root.appendChild(sjkm);
			root.appendChild(db);

			Element root2 = document.createElement("root");
			root2.setTextContent(r);
//			root.appendChild(name);
			root.appendChild(root2);

			Element pwd = document.createElement("pwd");
			pwd.setTextContent(pw);
//			root.appendChild(mima);
			root.appendChild(pwd);

			Element sqlORmysql = document.createElement("sqlORmysql");
			sqlORmysql.setTextContent(sq);
//			root.appendChild(xz);
			root.appendChild(sqlORmysql);

			Element print = document.createElement("print");
			print.setTextContent(pr);
//			root.appendChild(sc);
			root.appendChild(print);
			FileOutputStream os = null;
			try
			{
				os = new FileOutputStream("cfg/config.xml");
			}
			catch (Exception e)
			{
				
			    os = new FileOutputStream(GetRealPath.getRealpath() + "/config.xml");
			}
			
			TransformerFactory tf = TransformerFactory.newInstance();
			try
			{
				Transformer transformer = tf.newTransformer();
				DOMSource source = new DOMSource(document);
				transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
				PrintWriter pd = new PrintWriter(os);
				StreamResult result = new StreamResult(pd);
				transformer.transform(source, result);
				System.out.println("生成XML文件成功!");
			}
			catch (TransformerConfigurationException e)
			{
				System.out.println(e.getMessage());
			}
			catch (IllegalArgumentException e)
			{
				System.out.println(e.getMessage());
			}
			catch (TransformerException e)
			{
				System.out.println(e.getMessage());
			}

		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
